package com.cy.personer.web;

import com.cy.personer.bean.Combo;
import com.cy.personer.bean.Item;
import com.cy.personer.biz.ComboBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.ArrayList;

import static com.cy.personer.util.BarcodeUtil.generateFile;

@Controller
@RequestMapping("/combo")
public class ComboHandler {

    @Resource
    ComboBiz comboBizImpl;

    //查询套餐列表
    @RequestMapping(value = "/getCombos.action", method= RequestMethod.POST, produces="application/json;charset=utf-8")
    public @ResponseBody PageInfo<Combo> getCombos(Combo combo, int pageNum){
        return comboBizImpl.getCombos(combo,pageNum);
    }


    //查询套餐详情
    @RequestMapping(value = "/getCombo.action", method= RequestMethod.POST, produces="application/json;charset=utf-8")
    public @ResponseBody Combo getCombo(Combo combo){
        Combo combo1 = comboBizImpl.getCombo(combo);
        return combo1;
    }

    //删除套餐
    @RequestMapping(value = "/delCombo.action", method= RequestMethod.POST, produces="application/json;charset=utf-8")
    public @ResponseBody boolean delCombo(Combo combo){
        return comboBizImpl.delCombo(combo);
    }

    /**
     * 增加套餐
     * @param combo
     * @return
     * @author 林少艺
     */
    @RequestMapping(value = "/addCombo.action",method= RequestMethod.POST, produces="application/json;charset=utf-8")
    public @ResponseBody boolean addCombo(@RequestParam("itemIds")Integer[] itemIds, Combo combo){
        combo.setItems(new ArrayList<Item>());
        for(int i : itemIds){
            Item item = new Item();
            item.setItemId(i);
            combo.getItems().add(item);
        }
        return comboBizImpl.addCombo(combo);
    }

    //测试条形码
    @RequestMapping(value = "/barcode.action", method= RequestMethod.POST, produces="application/json;charset=utf-8")
    public @ResponseBody int barcode(int reportId,HttpSession session){
        String path = session.getServletContext().getRealPath("/barcode");
        File folder = new File(path);// 目录
        String keyword = ""+reportId+".png";//文件名
        //目录不存在
        if  (!folder.exists()  && !folder.isDirectory())
        {
            folder .mkdir();
            generateFile(reportId, path+"/"+keyword);
            //目录存在
        } else {
            File folder1 = new File(path+"/"+keyword);// 目录
            //文件不存在
            if(!folder1.exists())
            {
                generateFile(reportId, path+"/"+keyword);
            }
        }
        return reportId;
    }

}
